package com.example.hotel_lxy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hotel_lxy.R;
import com.example.hotel_lxy.utils.DateUtils;
import com.example.hotel_lxy.utils.LunarCalendars;
import com.example.hotel_lxy.utils.LunarHelper;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class RVAdapter2 extends RecyclerView.Adapter<RVAdapter2.ViewHolder> {

    private Context context;
    private List<String> data;
    private String stringq;
    private int type;
    public RVAdapter2(Context context, List<String> data,String stringq,int type){
        this.context = context;
        this.data = data;
        this.stringq= stringq;
        this.type= type;
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_rv2,parent,false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.name.setText(data.get(position));
        LunarCalendars lunarCalendar = LunarCalendars.getInstance();
        if(!data.get(position).equals("")){
            lunarCalendar.set(stringq+data.get(position)+"日");
            if(position%7==0||position%7==6){
                holder.name.setTextColor(context.getResources().getColor(R.color.color_F8525E));
            }else{
                holder.name.setTextColor(context.getResources().getColor(R.color.color_333333));
            }
            String[] split = stringq.replace("年", "-").replace("月", "-").split("-");
            String string = LunarHelper.SolarToLunarString(Integer.valueOf(split[0]), Integer.valueOf(split[1]), Integer.valueOf(data.get(position)));
            if(!lunarCalendar.getSolarFestival().equals("")){
                if(LunarHelper.Md(string)){
                    //设置公历节日
                    holder.name1.setText(string);
                }else{
                    //设置公历节日
                    holder.name1.setText(lunarCalendar.getSolarFestival());
                }

            }
            else if(!lunarCalendar.getLunarFestival().equals("")){
                //设置阴历节日
                if(LunarHelper.Md(string)){
                    //设置阴历节日
                    holder.name1.setText(string);
                }else{
                    //设置阴历节日
                    holder.name1.setText(lunarCalendar.getLunarFestival());
                }

            }
            else if(!lunarCalendar.getSolarTerms().equals("")){
                //设置阴历节日
                if(LunarHelper.Md(string)){
                    //设置24节气
                    holder.name1.setText(string);
                }else{
                    //设置24节气
                    holder.name1.setText(lunarCalendar.getSolarTerms());
                }
//                myViewHolder.tv_small.setText(lunarCalendar.getSolarTerms());
            }
            else{
                if(lunarCalendar.getLunarDate().equals("卅十")){
                    holder.name1.setText("三十");
                }else{
                    //设置普通阴历日期
                    if(LunarHelper.Md(string)){
                        //设置普通阴历日期
                        holder.name1.setText(string);
                    }else{
                        //设置普通阴历日期
                        holder.name1.setText(lunarCalendar.getLunarDate());
                    }
                }

            }
            String datas= stringq+data.get(position)+"日";
            final String replace = datas.replace("年", "-").replace("月", "-").
                    replace("日", "");
            DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-M-d");
            Date date = new Date(System.currentTimeMillis());//获取当前时间
            final String   currentDate = simpleDateFormat.format(date);

            if(replace.equals(currentDate)){
                holder.line1.setBackgroundColor(context.getResources().getColor(R.color.Color_15d37d));
                holder.name1.setText("今天");
                holder.name1.setTextColor(context.getResources().getColor(R.color.colorWhite));
                holder.name.setTextColor(context.getResources().getColor(R.color.colorWhite));
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(type!=2){
                        String data1 = DateUtils.data(replace);
                        String data = DateUtils.data(currentDate);
                        if(Long.valueOf(data)>Long.valueOf(data1)){
                            Toast.makeText(context, "过去的时间不能预定", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }

                    String datasq= stringq+ RVAdapter2.this.data.get(position)+"日";
                    String replaces = datasq.replace("年", "-").replace("月", "-").
                            replace("日", "");
                    mOnDeleteListener.delete(replaces);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private TextView name;
        private TextView name1;
        private LinearLayout line1;
        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            name1=itemView.findViewById(R.id.name1);
            line1=itemView.findViewById(R.id.line1);
        }
    }

    public interface OnDeleteListener{
        void delete(String position);
    }
    //声明接口对象
    private RVAdapter.OnDeleteListener mOnDeleteListener;

    //设置监听器,实例化接口
    public void setOnDeleteListener(RVAdapter.OnDeleteListener onDeleteListener){
        mOnDeleteListener = onDeleteListener;
    }
}

